package com.ebook.core.util;

import java.util.Date;
import java.util.Map;
import java.util.regex.Pattern;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.jdbc.core.JdbcTemplate;

import com.ebook.core.domain.Book;

public class BookUtil {
	
	private static Log logger = LogFactory.getLog(BookUtil.class);

	/**
	 * 从map中读取内容解析一本图书的基本信息(map一般是查询数据库返回的结果)
	 * @param map
	 * @return book如果正常，否则返回null
	 */

	public static Book parseBook(Map<String, Object> map) {
		try {
			Integer id = (Integer)map.get("id");
			String name = (String)map.get("name");
			String isbn = (String)map.get("isbn");
			String author = (String)map.get("author");
			String authors[] = author.split(";");	//多个作者名字用;分隔
			String translator = (String)map.get("translator");
			String translators[] = translator.split(";");	//多个译者名字用;分隔
			String press = (String)map.get("press");
			Date pressDate = (Date)map.get("press_date");
			String version = (String)map.get("version");
			Double fixedPrice = (Double)map.get("price_fixed");
			Double curPrice = (Double)map.get("price_cur");
			String cover_path = (String)map.get("cover_path");
			Integer status = (Integer)map.get("status");
			
			return new Book(id,name,isbn,authors,translators,press,pressDate.toString(),version,fixedPrice,curPrice,cover_path,status);
		} catch (Exception e) {
			logger.error("创建  Book 对象失败" + e.getMessage());
			return null;
		}	
	}

	
	
}
